package unused;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_deadfly implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);

		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}

		if (extra.length != 0) {
			if (extra[0] != " ") {
				String tosend = null;
				System.out.println("got this far....");
				PythonInterpreter interp = new PythonInterpreter();
				String python = null;

				// System.setProperty("java.security.policy","test.policy");

				// SecurityManager sm = new SecurityManager();

				// System.setSecurityManager(sm);

				// interp.exec(" import sys");
				Writer out = new StringWriter();
				interp.setOut(out);
				interp.set("url", extra[0]);
				interp.set("nyud", ".nyud.net");
				interp.exec("import urlparse,urllib2,re,sys");
				interp.exec("l=list(urlparse.urlsplit(url))");
				interp.exec("l[1]+=nyud");
				interp.exec("url=urlparse.urlunsplit(l)");
				interp.exec("print re.search(r'var url = \\'([^\\']+)\\'', urllib2.urlopen(url).read()).group(1)");
				String result = out.toString();

				// interp.exec("sys.stdout = StringIO()");
				// interp.exec(python);
				// interp.exec("sys.stdout.seek(0); outputtext = sys.stdout.read();");
				// PyObject x = interp.get("outputtext");

				// TODO Auto-generated catch block

				tosend = result;

				//
				// tosend = " seems to be up.";

				
				irc.sendMessage(channel, start + "" + tosend);
			} else {
				
				irc.sendMessage(channel, start
						+ "You need to specify an input!");
			}
		} else {
			
			irc.sendMessage(channel, start + "You need to specify an input!");
		}
	}

	public String help() {
		return "Deadfly - deadfly [adf.ly link] - Gets the end result of an adf.ly link.";
		// TODO Auto-generated method stub

	}

	// public static boolean exists(String URLName){
	// try {
	// HttpURLConnection.setFollowRedirects(true);
	// // note : you may also need
	// //HttpURLConnection.setInstanceFollowRedirects(false)
	// HttpURLConnection con =
	// (HttpURLConnection) new URL(URLName).openConnection();
	// con.setRequestMethod("HEAD");
	// return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	// }
	// catch (Exception e) {
	// e.printStackTrace();
	// return false;
	// }
	// }
	public String version() {
		return "Py version 1";
		// TODO Auto-generated method stub

	}

	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "df, dead, dfly, deadf";
	}

}
